package com.woniu.mall.controller;

import com.woniu.common.annotation.LoginUser;
import com.woniu.common.entity.Result;
import com.woniu.common.entity.ResultCode;
import com.woniu.mall.DTO.SeckillDTO;
import com.woniu.mall.constant.OrderConstants;
import com.woniu.mall.constant.SeckillRedisKey;
import com.woniu.mall.pojo.MallSeckill;
import com.woniu.mall.service.IMallSeckillService;
import com.woniu.mall.service.impl.MultiCreateOrder;
import com.woniu.user.vo.UserLoginVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 秒杀活动表 前端控制器
 * </p>
 *
 * @author Klaus
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/mallSeckill")
public class MallSeckillController {
    @Autowired
    private IMallSeckillService seckillService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MultiCreateOrder multiCreateOrder;

    @ApiOperation("多线程秒杀")
    @GetMapping("/multiSeckill/{productId}")
    public Result multiSeckill(@PathVariable Integer productId, @LoginUser UserLoginVO userLoginVO){
        //防止用户重复排队问题
        long increment = redisTemplate.boundHashOps(SeckillRedisKey.SECKILL_ORDER_COUNT).increment(userLoginVO.getMobile(), 1);
        if (increment != 1){
            //表示用户已经重复排队
            return Result.error(ResultCode.REPEAT_REQUEST.getCode(), "商品重复购买");
        }

        //防止超卖问题
        Object obj = redisTemplate.boundListOps(SeckillRedisKey.SECKILL_ORDER_OVER).rightPop();
        if (obj == null){
            //没有获取到令牌
            return Result.error(ResultCode.PRODUCT_NO_STOCK.getCode(), "商品库存不足");
        }

        //构建秒杀业务对象
        SeckillDTO seckillDTO = new SeckillDTO();
        seckillDTO.setUserId(userLoginVO.getId());
        seckillDTO.setMobile(userLoginVO.getMobile());
        seckillDTO.setProductId(productId);
        seckillDTO.setStatus(OrderConstants.SECKILL_STATUS_START);
        //将该秒杀对象添加到redis的队列中
        redisTemplate.boundListOps(SeckillRedisKey.SECKILL_ORDER_QUEUE).leftPush(seckillDTO);
        //创建一个秒杀线程进行下单操作
        String form = multiCreateOrder.createSeckillOrder();
        return Result.ok(form);
    }


    @ApiOperation("查询秒杀排队状态")
    @PostMapping("/status")
    public Result<SeckillDTO> getSeckillStatus(@LoginUser UserLoginVO userLoginVO){
        SeckillDTO seckillDTO = (SeckillDTO) redisTemplate.boundHashOps(SeckillRedisKey.SECKILL_ORDER_STATUS).get(userLoginVO.getMobile());
        return Result.ok(seckillDTO);
    }

    @ApiOperation("单线程秒杀")
    @GetMapping("/create/{productId}")
    public Result seckill(@PathVariable Integer productId, @LoginUser UserLoginVO userLoginVO){
        seckillService.seckill(productId, userLoginVO);
        return Result.ok();
    }

    @ApiOperation("获取秒杀商品(从数据库)")
    @GetMapping("/listSeckill")
    public Result<List<MallSeckill>> listStarting(){
        List<MallSeckill> seckillList = seckillService.listStarting();
        return Result.ok(seckillList);
    }

    @ApiOperation("添加秒杀优惠券到")
    @GetMapping("/addStartingCoupon")
    public Result addStartingCoupon(String zoneCouponType, BigDecimal amount, BigDecimal price, Integer quantity, Date startTime, Date endTime){
        seckillService.addStartingCoupon(zoneCouponType, amount, price, quantity, startTime, endTime);
        return Result.ok();
    }



}
